package com.yuantong.xyerp.core.provider.dao;

import com.yuantong.xyerp.core.api.dto.ContractTransportationDto;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 合同和调拨运输信息 Mapper 接口
 * </p>
 *
 * @author JianguoYin
 * @since 2021-07-21
 */
public interface ContractTransportationMapper {

    /**
     * 用于 根据选择栏条件全部导出; (单独一个按钮)
     * @param businessType b
     * @param startTime s
     * @param endTime e
     * @param fuzzy f
     * @return r
     */
    @Select({"<script> " +
            "SELECT ct.*, c.create_time AS business_create_time, c.contract_no, c.doc_no, c.business_type, " +
            "CONCAT(a.name,'-',a1.name) AS transport_path " +
            "FROM t_contract_transportation ct " +
            "LEFT JOIN t_contract c ON (c.id = ct.source_id) " +
            "LEFT JOIN t_area a ON (a.code = ct.start_area_code) " +
            "LEFT JOIN t_area a1 ON (a1.code = ct.destination_area_code) " +
            "WHERE 1=1 " +
            "<if test=\"businessType != null and businessType != ''\"> " +
            "    AND c.business_type = #{businessType} " +
            "</if>" +
            "<if test=\"startTime != null\"> " +
            "  AND ct.create_time &gt;= #{startTime} " +
            "</if>" +
            "<if test=\"endTime != null\"> " +
            "  AND ct.create_time &lt;= #{endTime} " +
            "</if> " +
            "<if test=\"fuzzy != null and fuzzy != ''\"> " +
            "<bind name=\"bindKey\" value=\"'%'+fuzzy+'%'\"/> " +
            "AND (c.contract_no LIKE #{bindKey} OR c.doc_no LIKE #{bindKey} ) " +
            "</if> " +
            "<if test=\"ids != null and ids.size() > 0\"> " +
            "AND ct.id IN " +
            "<foreach collection=\"ids\" item=\"id\" open=\"(\" close=\")\" separator=\",\"> " +
            "#{id} " +
            "</foreach> " +
            "</if> " +
            "ORDER BY c.create_time DESC " +
            "</script>"})
    @ResultMap("ContractTransportationResult")
    List<ContractTransportationDto> findByFuzzy(String businessType, Date startTime, Date endTime, String fuzzy,
                                                List<String> ids);


}
